#!/usr/bin/env python
from lib.format_time import *
from lib.handle_mongo import *
from lib.switch_code import *
import re
import sys

       
def format_keyword(keyword):
    keyword=re.sub(r'\n|\\|\'|\"|\||\\','',keyword)
    keyword=re.sub(r'\s+',' ',keyword)
    return keyword

def decode_keyword(keyword):
    import perl
    INC=perl.get_ref("@INC")
    INC.append("/work/opt/analytic/lib")
    perl.require("HandleCode")
    return perl.call("HandleCode::decode_url",keyword)

def get_search_info(date,file):
    db=connect_db(host='10.3.12.16',port=58885,db='groupbuy',user='groupbuyuser',passwd='a8a7h@ft6^%39')
    table="session_58tg_%s" % date
    rex='^http://(.*\.)?(t|tuan)\.58\.com/([^\/]+)\/g_s\/([^\/\?]+)'
    items=find_items(db,table,condition={"$or":[{"sessions":{"$elemMatch":{"kw":{"$ne":''}}}},{"sessions":{"$elemMatch":{"actions":{"$elemMatch":{"url":re.compile(rex)}}}}}]})
    info={}
    stat_date=format_date(date,sep='')
    sep='|'
    f=open(file,'w')
    for item in items:
        for ses in item['sessions']:
            if ses.has_key('kw'):
                keyword=format_keyword(ses['kw'])
	        if keyword.find("http://")!=-1:
                    continue
                if keyword.find('%')!=-1:
                    keyword=decode_keyword(keyword)
                domain=ses['refdomain']
                try:
		    key=sep.join([keyword,domain,''])
		except:
		    print keyword,domain
                if info.has_key(key):
                    info[key]+=1
                else:
                    info[key]=1
                #print key
            for act in ses['actions']:
                m=re.match(rex,act['url'])
                if m:
                    keyword=format_keyword(m.group(4))
                    if keyword.find('%')!=-1:
                        keyword=decode_keyword(keyword)
                else:
                    continue
                if act.has_key('i58'):
                    user_id=act['i58']
                else:
                    user_id=''
                key=sep.join([keyword,'t.58.com',str(user_id)])
                #print key
                if info.has_key(key):
                    info[key]+=1
                else:
                    info[key]=1
    for key,value in info.items():
        f.write(sep.join([str(stat_date),key,str(value)])+"\n")
        
def main():
    if len(sys.argv)==1:
        start_date=get_yesterday()
        end_date=start_date
    elif len(sys.argv)==2:
        start_date=format_date(sys.argv[1])
        end_date=start_date
    elif len(sys.argv)==3:
        start_date=format_date(sys.argv[1])
        end_date=format_date(sys.argv[2])
    stat_date=start_date
    while stat_date>=start_date and stat_date<=end_date:
        print "The program of the date of %s is beginning at %s " %(stat_date,get_now())
        file='/work/opt/analytic/search_keyword/mid_file/search_%s.csv' % (format_date(stat_date,sep=''))
        get_search_info(stat_date,file)
        print "The program of the date of %s is end at %s " %(stat_date,get_now())
        stat_date=var_date(stat_date)
    
if __name__=='__main__':
    main()

